package com.huang.controller;

import java.util.Collections;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huang.base.BaseController;
import com.huang.entity.UserPo;
import com.huang.resp.CommonResult;
import com.huang.service.UserService;
import com.huang.validation.SaveGroupInterface;
import com.huang.validation.UpdateGroupInterface;
import com.huang.vo.Pager;

import lombok.extern.log4j.Log4j2;

/**
 * 用户管理模块
 *
 * @author han
 * @since 2022-05-26 22:40:08
 */
@Log4j2
@RestController
@RequestMapping("user")
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    @GetMapping("list")
    public CommonResult list(@RequestBody(required = false) Pager<UserPo> pager) {
    	log.info("用户管理列表入参：{}", pager);
        QueryWrapper<UserPo> queryWrapper = new QueryWrapper<>();
        pager = (Pager<UserPo>) parse(pager, queryWrapper);
        pager = userService.page(pager, queryWrapper);
        return CommonResult.success(pager);
    }

    @PostMapping("add")
    public CommonResult add(@RequestBody @Validated(SaveGroupInterface.class) UserPo userPo) {
        boolean save = userService.saveUserPro(userPo);
        return save ? CommonResult.success() : CommonResult.error("保存用户失败");
    }

    @PostMapping("update")
    public CommonResult update(@Validated(UpdateGroupInterface.class) @RequestBody UserPo userPo) {
        boolean update = userService.updateByUser(userPo);
        return update ? CommonResult.success() : CommonResult.error("修改用户失败");
    }

    @PostMapping("delete")
    public CommonResult delete(String ids) {
        boolean remove = userService.removeByIds(Collections.singletonList(ids));
        return remove ? CommonResult.success() : CommonResult.error("删除用户失败");
    }

}
